
$(function() {
	
	var $checkboxAccount = $("#checkboxAccount");
	var $checkboxOther = $("#checkboxOther");
	var $check = $(".isVoucher");
	
	$checkboxAccount.on("click",function(){
		var $this = $(this);
		var accountMoney = $("#accountMoney")[0].innerText;
		var orderMoney = $("#orderMoney")[0].innerHTML;
		var voucherMoney = $('#voucherMoney')[0].innerHTML;
		if(!$this.prop("checked")){
			if(voucherMoney != "")	orderMoney = orderMoney - Number(voucherMoney);
			$('#payByAccountMoney').html(0.00);
			$('#checkboxOther').prop("checked",true);
			if(Number(orderMoney) < 0) orderMoney = 0.00;
			$('#payByOther').html(parseFloat(Number(orderMoney)).toFixed(2));
		}else{
			assignAutoComputer(voucherMoney);
		}
	})
	
	$checkboxOther.on("click",function(){
		var $this = $(this);
		var accountMoney = $("#accountMoney")[0].innerText;
		var orderMoney = $("#orderMoney")[0].innerHTML;
		var voucherMoney = $('#voucherMoney')[0].innerHTML;
		if(!$this.prop("checked")){
			if(voucherMoney != "")	orderMoney = orderMoney - Number(voucherMoney);
			$('#payByOther').html(0.00);
			$('#checkboxAccount').prop("checked",true);
			if(Number(orderMoney) < 0) orderMoney = 0.00;
			$('#payByAccountMoney').html(parseFloat(Number(orderMoney)).toFixed(2));
		}else{
			assignAutoComputer(voucherMoney);
		}
	})
	
	$check.on("click",function(){
		var $this = $(this);
		if(!$this.prop("checked")){
			$('#voucherNum').prop("readOnly",true);
			$('#voucherNum').val('请输入代金券编号');
			$('#voucherMoney').html(0.00);
			var voucherMoney = 0.00;
			$('#showVoucherMoney').prop("hidden",true);
			assignAutoComputer(voucherMoney);
		}else{
			$('#voucherNum').prop("readOnly",false);
			$('#voucherNum').val('');
			$('#showVoucherMoney').prop("hidden",false);
		}
	})
	
	$('#voucherNum').on("blur",function(){
		if($('#voucherNum').prop("readOnly")) return;
		
		var orderId = $('input[name="orderId"]').val();
		var voucherNum = $('#voucherNum').val();
		voucherNum = $.trim(voucherNum);
		if(voucherNum == "") {
			BSTYUN.dialog.ok('请输入代金券编号！');
			return;
		}
		
		$.ajax({
			type:"POST",
			url:"/buy/checkVoucherNum",
			async:false,
			data:{
				voucherNum:voucherNum,
				dtype:'xf',
				bType:orderId  
			},
			success:function(data,response){
				
				if(data.vIsExist == true){
					if(data.vTypeFlag == true){
						if(data.bTypeFlag == true){
							if(data.bInfoFlag == true){
								var voucher = data.voucher;
								$('#voucherMoney')[0].innerHTML = voucher.voucherMoney;
								$('#voucherMoney').html(voucher.voucherMoney);
								assignAutoComputer(voucher.voucherMoney);
							}else{
								BSTYUN.dialog.ok("该代金券绑定信息与当前用户信息或所购买产品信息不一致！请输入正确的代金券编号！");
								$('#voucherNum').val('');
								$('#voucherMoney').html(0.00);
							}
						}else{
							BSTYUN.dialog.ok("该代金券绑定类型与当前用户类型或所购买产品信息不一致！请输入正确的代金券编号！");
							$('#voucherNum').val('');
							$('#voucherMoney').html(0.00);
						}
					}else{
						BSTYUN.dialog.ok("该代金券类型与当前使用类型不一致！请输入正确的代金券编号！");
						$('#voucherNum').val('');
						$('#voucherMoney').html(0.00);
					}
				}else{
					BSTYUN.dialog.ok("该代金券不存在！请输入正确的代金券编号！");
					$('#voucherNum').val('');
					$('#voucherMoney').html(0.00);
				}
			}
		})
	})
});

//计算 支付金额 
function assignAutoComputer(voucherMoney){
	var accountMoney = $("#accountMoney")[0].innerText;
	var orderMoney = $("#orderMoney")[0].innerHTML;
	var voucherMoney = $('#voucherMoney')[0].innerHTML;
	//代金券抵消订单总额
	if(Number(voucherMoney) != 0 && voucherMoney) orderMoney = Number(orderMoney) - Number(voucherMoney);
	if(Number(orderMoney) < 0) orderMoney = 0.00;
	
	if(Number(accountMoney) > 0 ){
		$('#checkboxAccount').prop("checked",true);
		if(Number(accountMoney) >= Number(orderMoney)){
			$("#payByAccountMoney").html(parseFloat(Number(orderMoney)).toFixed(2));
			$("#loginUserPrice").html(parseFloat(Number(orderMoney)).toFixed(2));
			
			//余额足够的话，必须去掉账户余额才能选择第三方支付
//			$('#checkboxOther').prop("checked",false);
			
			$("#payByOther").html(0.00);
		}else{
			var money = Number(orderMoney) - Number(accountMoney) ;
			$("#payByAccountMoney").html(parseFloat(Number(accountMoney)).toFixed(2));
			$('#checkboxOther').prop("checked",true);
			$("#payByOther").html(parseFloat(Number(money)).toFixed(2));
		}
	}else{
		$("#payByAccountMoney").html(0.00);
		$('#checkboxOther').prop("checked",true);
		$("#payByOther").html(parseFloat(Number(orderMoney)).toFixed(2));
		$("#loginUserPrice").html(parseFloat(Number(orderMoney)).toFixed(2));
	}
	
}

//支付
function payOrder(){
	var orderId = $('input[name="orderId"]').val();
	var orderNum = $('input[name="orderNum"]').val();
	var orderMoney = $('#orderMoney')[0].innerHTML;
	var accountMoney = $('#accountMoney')[0].innerHTML;
	var userId =  $('input[name="userId"]').val();
	var proMealName = $('input[name="proMealName"]').val();

	var payByAccountMoney = $('#payByAccountMoney')[0].innerHTML;
	var payByOther = $('#payByOther')[0].innerHTML;
	
	var $checkboxAccount = $("#checkboxAccount");
	var $checkboxOther = $("#checkboxOther");
	var $check = $(".isVoucher");
	var $alipayRadio = $("#alipayRadio");
	var $chinapayRadio = $("#chinapayRadio");
	var $chinapayB2BRadio = $("#chinapayB2BRadio");
	var $chinapayB2CRadio = $("#chinapayB2CRadio");
	
	var voucherNum = $.trim($('#voucherNum').val());
	var voucher = 0;
	
	if($check.prop("checked")){
		voucher = voucherNum;
	}
	
	//未选择任何一种方式付款
	if(!$checkboxAccount.prop("checked") && !$checkboxOther.prop("checked")){
		BSTYUN.dialog.ok("请选择一种方式进行付款！");
		return;
	
	//选择账户余额支付
	}else if($checkboxAccount.prop("checked") && !$checkboxOther.prop("checked")){
		payByAccount(accountMoney,orderMoney,orderId,proMealName,voucher);
		
	//选择第三方支付
	}else if(!$checkboxAccount.prop("checked") && $checkboxOther.prop("checked")){
		// 使用支付宝付款
		if($alipayRadio.prop("checked")){
			location.href="/alipay/doPay?orderNum="+orderNum+"&userId="+userId
			+"&proMealName="+proMealName+"&orderId="+orderId+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&voucher="+voucher;
			
		}else if($chinapayRadio.prop("checked")){
			location.href="/chinapay/payByNoCard?orderNum="+orderNum+"&orderId="+orderId
			+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&userId="+userId+"&voucher="+voucher;
		
		}
		else if($chinapayB2BRadio.prop("checked")){
			location.href="/chinapay/payByB2B?orderNum="+orderNum+"&orderId="+orderId
			+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&userId="+userId+"&voucher="+voucher;
		
		}else if($chinapayB2CRadio.prop("checked")){
			location.href="/chinapay/payByB2C?orderNum="+orderNum+"&orderId="+orderId
			+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&userId="+userId+"&voucher="+voucher;
		
		}
		
	//两种方式都选择
	}else if($checkboxAccount.prop("checked") && $checkboxOther.prop("checked")){
		// 支付宝付款
		if($alipayRadio.prop("checked")){
			if(Number(payByOther) == 0){
				payByAccount(accountMoney,orderMoney,orderId,proMealName,voucher);
			}else{
				location.href="/alipay/doPay?orderNum="+orderNum+"&userId="+userId
				+"&proMealName="+proMealName+"&orderId="+orderId+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&voucher="+voucher;
			}
		}else if($chinapayRadio.prop("checked")){
			location.href="/chinapay/payByNoCard?orderNum="+orderNum+"&orderId="+orderId
			+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&userId="+userId+"&voucher="+voucher;
		
		}
		else if($chinapayB2BRadio.prop("checked")){
			location.href="/chinapay/payByB2B?orderNum="+orderNum+"&orderId="+orderId
			+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&userId="+userId+"&voucher="+voucher;
		
		}else if($chinapayB2CRadio.prop("checked")){
			location.href="/chinapay/payByB2C?orderNum="+orderNum+"&orderId="+orderId
			+"&payByAccountMoney="+parseFloat(payByAccountMoney).toFixed(2)+"&userId="+userId+"&voucher="+voucher;
		
		}
		
	}
	
}

//余额支付
function payByAccount(accountMoney,orderMoney,orderId,proMealName,voucher){
	
	var voucherMoney = $('#voucherMoney')[0].innerHTML;
	//代金券抵消订单总额
	if(Number(voucherMoney) != 0 && voucherMoney) orderMoney = Number(orderMoney) - Number(voucherMoney);
	if(Number(orderMoney) < 0) orderMoney = 0.00;
	
	if(Number(accountMoney) < Number(orderMoney)) {
		BSTYUN.dialog.ok("账户余额不足，请充值或者选择其他方式付款！");
	}else{
		var loading = function(html,callback){
			        var dialog = null;
			        dialog = $.dialog({
			            id: 'loading',
			            fixed:true,
			            title:false,
			            lock:true,
			            padding:'10px 20px',
			            content: '<div class="loading-tip">'+html+'</div>'
			        });
			    }
		loading('<span style="color:red;font-size:16px;">数据正在同步,时长约为1分钟!请耐心等待!<br>请勿刷新该页面，否则数据同步将会失败！</span>');
		location.href="/buy/payOrder/"+orderId+"/"+parseFloat(orderMoney).toFixed(2)+"/"+proMealName+"/"+voucher;
	}
}


//判断邮箱
var isEmail = function(str) {
    var emailReg = /^[a-z0-9][\w\.]*@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,5}$/i;
    return emailReg.test(str);
}
var isMobile = function(str){
	var mobileReg = /^1[345678][0-9]{9}$/;
	return mobileReg.test(str);
}


$(function(argument) {
	var $next = $("#next"),
		$orderLinkEmail = $("#orderLinkEmail"),
		$loginUserUnitAllName = $("#loginUserUnitAllName"),
		$orderLinkMan = $("#orderLinkMan"),
		$orderLinkPhone = $("#orderLinkPhone"),
		$panel = $(".panel"),
		$back = $("#back"),
		$submit = $("#submit");
		$check = $(".isVoucher");
	$next.add($back).on("click",function(){
		$panel.toggle();
	});
	$submit.on('click', function(event) {
		var orderLinkEmail = $.trim($orderLinkEmail.val()),
			loginUserUnitAllName = $.trim($loginUserUnitAllName.val()),
			orderLinkMan = $.trim($orderLinkMan.val()),
			orderLinkPhone = $.trim($orderLinkPhone.val()),
			proMealName = $('input[name="proMealName"]').val(),
			orderId = $('input[name="orderId"]').val(),
		
		la_length = loginUserUnitAllName.length;
		if (la_length == 0) {
			$('.domain_error').eq(0).html('单位全称不能为空!');
			$('.domain_error').eq(1).html('');
			$('.domain_error').eq(2).html('');
			$('.domain_error').eq(3).html('');
			return false;
		}else{
			
		}	
		
		ol_length = orderLinkMan.length;
		if (ol_length == 0) {
			$('.domain_error').eq(0).html('');
			$('.domain_error').eq(1).html('联系人不能为空!');
			$('.domain_error').eq(2).html('');
			$('.domain_error').eq(3).html('');
			return false;
		}else{
			
		}
		
		if (isMobile(orderLinkPhone)) {
			
		}else{
			$('.domain_error').eq(0).html('');
			$('.domain_error').eq(1).html('');
			$('.domain_error').eq(2).html('联系电话格式错误!');
			$('.domain_error').eq(3).html('');
			return false;
		}
		
		if (isEmail(orderLinkEmail)) {

		}else{
			$('.domain_error').eq(0).html('');
			$('.domain_error').eq(1).html('');
			$('.domain_error').eq(2).html('');
			$('.domain_error').eq(3).html('邮箱格式错误');
			return false;
		}
		
		var voucherNum = $.trim($('#voucherNum').val());
		var voucher = 0;
		
		if($check.prop("checked")){
			voucher = voucherNum;
		}
		
		//发送邮件给客户    
		$.ajax({
			type:"POST",
			url:"/buy/orderSendEmailToUser",
			async:false,
			data:{
				orderLinkEmail:orderLinkEmail,
				loginUserUnitAllName:loginUserUnitAllName,
				orderLinkMan:orderLinkMan,
				orderLinkPhone:orderLinkPhone,
				proMealName:proMealName,
				orderId:orderId,
				voucherNum:voucher
			},
			success:function(data,response){
				if(data){
					BSTYUN.dialog.ok("您的资料已提交，请按照提交内容进行付款，款到后次日五点前将为您开通相关服务，谢谢！<br>如有疑问请拨打我们的服务电话：400-8078-100",function(){
						location.href ="/consume/order/manage";
					});
				}else{
					BSTYUN.dialog.warn("邮件发送失败，请重新发送！");
				}
			}
		})
	});

});

